package com.bole.service.impl;

import com.bole.dao.CollectionsDao;
import com.bole.dto.collections.CollectionsDto;
import com.bole.pojo.CollectionsDo;
import com.bole.service.CollectionsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CollectionsServiceImpl implements CollectionsService {

    @Resource
    private CollectionsDao collectionsDao;

    /**
     * 保存
     * @param collectionsDto
     */
    @Override
    public void saveCollections(CollectionsDto collectionsDto) {
        CollectionsDo collectionsDo = new CollectionsDo();
        BeanUtils.copyProperties(collectionsDto,collectionsDo);
        collectionsDo.setStatus(0);
        collectionsDao.save(collectionsDo);
    }

    @Override
    public boolean isCollection(Long carId, Long userId) {
        CollectionsDo collectionsDo = collectionsDao.findByCarIdAndUserId(carId, userId);
        if (null == collectionsDo) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 根据userid查询收藏记录
     * @param userId
     * @return
     */
    @Override
    public List<CollectionsDo> findCollection(Long userId) {

        return collectionsDao.findByUserIdAndStatus(userId,0);
    }

    /**
     * 取消收藏
     * @param carId
     * @param userId
     */
    @Override
    public void deleteCollections(Long carId, Long userId) {
        CollectionsDo collectionsDo = collectionsDao.findByCarIdAndUserId(carId, userId);
        collectionsDao.deleteById(collectionsDo.getId());
    }
}
